#!/bin/sh

#-------------------------------------#
#  SGT (Software Generation Toolkit)  #
#-------------------------------------#
#                                     #
#      Get info from file             #
#      (size, date and time)          #
#                                     #
#  GDE 08/12/03 v1.0                  #
#       creation                      #
#                                     #
#-------------------------------------#
# $1: binary file                     #
# $2: output file                     #
# $3: log file                        #
#-------------------------------------#


#-------------------------------------#
# Script informations
#-------------------------------------#
export SCRIPT_SGT_VER=1.0
export SCRIPT_SGT_DATE=08/12/2004
export SCRIPT_SGT_USER=GDE
export SCRIPT_SGT_NAME=_get_file_info
export SCRIPT_SGT_USAGE="Get info from file (size, date and time)"
#-------------------------------------#



echo " "



	
#-------------------------------------#
# Parameters section
#-------------------------------------#
#---------------------
# Parameter 3 -> log file
#---------------------
if [ ! -z $3 ]
then
	export STATUS_FILE=$3
else
	# The log file is not defined
	export MSG_ERROR_SCRIPT=1
	export CMD_SCRIPT=Error
fi


#---------------------
# Parameter 2 -> input file
#---------------------
if [ ! -z $2 ]
then
	export COMMENT_ZIP_FILE=$2
else
	# The comment zip file is not defined
	export MSG_ERROR_SCRIPT=2
	export CMD_SCRIPT=Error
fi


#---------------------
# Parameter 1 -> full tool path / specific option
#---------------------
if [ ! -z $1 ]
then
	if [ $1 = -v ]
	then
		export CMD_SCRIPT=Version
	else
		if [ $1 = -h ]
		then
			export CMD_SCRIPT=Help
		else
			if [ $1 = -u ]
			then
				export CMD_SCRIPT=Usage
			else
				export BINARY_FILE=$1
			fi
		fi
	fi
fi


#-------------------------------------#
# if no option is defined, display help
#-------------------------------------#
if [ $# -eq 0 ]
then
	export CMD_SCRIPT=Help
fi


#-------------------------------------#
# Process section
#-------------------------------------#
if [ -z $CMD_SCRIPT ] && [ ! -z $STATUS_FILE ] && [ ! -z $COMMENT_ZIP_FILE ] && [ ! -z $BINARY_FILE ]
then

	size_file=`ls -l $BINARY_FILE | cut -b36-43`
	echo "Size = $size_file" >> $COMMENT_ZIP_FILE
	
	full_time=`ls -F --full-time $BINARY_FILE | cut -b44-68`
	
	date_file=`date -d "$full_time" +%x | sed "s/\///g"`
	echo "Date = $date_file" >> $COMMENT_ZIP_FILE
	
	time_file=`date -d "$full_time" +%T`
	echo "Time = $time_file" >> $COMMENT_ZIP_FILE

	exit
fi


echo " "


#-------------------------------------#
# Help script section
#-------------------------------------#
if [ -z $CMD_SCRIPT ]
then
	export CMD_SCRIPT=Version
fi

#---------------------
# Usage
#---------------------
if [ $CMD_SCRIPT = Usage ]
then
	echo "  $SCRIPT_SGT_NAME    $SCRIPT_SGT_USAGE"
	exit
fi

#---------------------
# Version
#---------------------
echo "-------------------------------------"
echo " '$SCRIPT_SGT_NAME' (release "$SCRIPT_SGT_VER") for SGT "$SGT_VER". Last update by "$SCRIPT_SGT_USER" on "$SCRIPT_SGT_DATE
echo "  $SCRIPT_SGT_USAGE"
if [ $CMD_SCRIPT = Version ]
then
	exit
fi

#---------------------
# Simple help
#---------------------
if [ $CMD_SCRIPT = Help ]
then
	echo "-------------------------------------"
	echo " $SCRIPT_SGT_NAME [INPUT_FILE] [OUTPUT_FILE]"
	echo " "
	echo " "
	echo "  -h | --help  Display this help "
	echo "  -v           Display the version of the current script"
fi


#-------------------------------------#
# Error messages section
#-------------------------------------#
if [ $CMD_SCRIPT = Error ]
then
	if [ $MSG_ERROR_SCRIPT = 1 ]
	then
		echo "-------------------------------------"
		echo " "
		echo "ERROR : The log file is not defined !"
		echo "-------------------------------------" >> $STATUS_FILE
		echo " " >> $STATUS_FILE
		echo "ERROR : The log file is not defined !" >> $STATUS_FILE
	fi
	if [ $MSG_ERROR_SCRIPT = 2 ]
	then
		echo "-------------------------------------"
		echo " "
		echo "ERROR : The comment zip file is not defined !"
		echo "-------------------------------------" >> $STATUS_FILE
		echo " " >> $STATUS_FILE
		echo "ERROR : The comment zip file is not defined !" >> $STATUS_FILE
	fi
	echo " "
	echo " Check your command line or see help section -> $SCRIPT_SGT_NAME -h"
	echo "-------------------------------------"
fi

